package com.powergisol.gis.service;


import com.powergisol.gis.dao.RoleDao;
import com.powergisol.gis.dao.UserDao;
import com.powergisol.gis.model.RoleModel;
import com.powergisol.gis.model.SysUser;
import com.powergisol.gis.model.UserModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author yaoxin
 * @description 登录校验
 * @date
 */
@Service
@Slf4j
public class MyUserDetailsServiceImpl implements UserDetailsService {

    @Resource
    private UserDao userDao;


    @Override
    public UserDetails loadUserByUsername(String s) {
        log.info("用户登录传参==" + s);

        UserModel userModel = this.userDao.findByUsername(s);
        if (userModel == null) {
            userModel = this.userDao.findByPhone(s);
        }
        if (userModel == null) {
            log.error("帐号未找到=" + s);
            throw new UsernameNotFoundException("帐号未找到=" + s);
        }

        List<? extends GrantedAuthority> authorities = new ArrayList();

        return  new SysUser(userModel.getId(),userModel.getNickName(),userModel.getUsername(),userModel.getPassword(),authorities);

    }

    public static void main(String[] args) {
    }

}
